package ytr.android.sparrow;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ytr.android.util.ListCalculation;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.WrapperListAdapter;

/**
 * 麻雀スコア管理画面。
 * @author toki
 *
 */
public class ScoreManagementActivity extends ScoreManagementBaseActivity {


	/**
	 * 別スレッドからの描画要求対応用ハンドラ
	 */
	public DrawHandler m_DrawHandler;

	/**
	 * スコアリストビュー
	 */
	protected ListView m_ScoreListView;

	/**
	 * リザルトリストビュー
	 */
	protected ListView m_ResultListView;

	/**
	 * 初期情報設定画面の設定値取得用オブジェクト
	 */
	public SharedPreferences m_Preference;

	/**
	 * リスト計算用ユーティリティクラス
	 */
	ListCalculation m_listCalculation;

	/**
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.score_management);

		setUtilityObject();
		setHandler();
		getPreference();

		getListControl();

		initScoreListHeader();
		initScoreListFooter();
		initScoreListBody();

		initResultList();

		setScoreListListener();
		setResultListListener();
	}

	/**
	 * 
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu_management, menu);    	
		return true;
	}

	/**
	 * 
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		/*
		 * 設定ボタン
		 */
		case R.id.setting_button :
			startInitSettingActivity();
			break;
			/*
			 * 符計算ボタン
			 */
		case R.id.hu_calc_table_button :
			startHuCalcTableActivity();
			break;
		default:
			Log.w(getCommonLogTag(), "onOptionsItemSelected Unknown Resource ID");
		}
		return true;
	}

	private void setUtilityObject() {
		m_listCalculation = new ListCalculation();		
	}

	private void getPreference() {
		m_Preference = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
	}

	private void getListControl() {
		m_ScoreListView = (ListView) findViewById(R.id.scoreList);
		m_ResultListView = (ListView) findViewById(R.id.resultList);
	}

	private void initScoreListHeader() {
		
		/*
		 * add Header
		 */
		LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View headerView = inflater.inflate(R.layout.score_management_header, null);
		m_ScoreListView.addHeaderView(headerView);

		/*
		 * set UserName
		 */
		TextView userName1 = (TextView) findViewById(R.id.listHeader_Text1);
		TextView userName2 = (TextView) findViewById(R.id.listHeader_Text2);
		TextView userName3 = (TextView) findViewById(R.id.listHeader_Text3);
		TextView userName4 = (TextView) findViewById(R.id.listHeader_Text4);
		TextView userName5 = (TextView) findViewById(R.id.listHeader_Text5);
		TextView userName6 = (TextView) findViewById(R.id.listHeader_Text6);

		SparrowDBHelpter dbHelper = new SparrowDBHelpter(this);
		List<UserSettingItem> userList = dbHelper.getUserNameTableData();
		if (userList.size() == LIST_VARIABLE_COLUMN_NUM) {
			userName1.setText(userList.get(0).getName());
			userName2.setText(userList.get(1).getName());
			userName3.setText(userList.get(2).getName());
			userName4.setText(userList.get(3).getName());
			userName5.setText(userList.get(4).getName());
			userName6.setText(userList.get(5).getName());
		}
	}

	private void initScoreListFooter() {

		LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View footerView = inflater.inflate(R.layout.score_management_footer, null);
		m_ScoreListView.addFooterView(footerView);

		TextView footerCellLabel = (TextView) footerView.findViewById(R.id.listFooter_Label);
		TextView footerCell1 = (TextView) footerView.findViewById(R.id.listFooter_Text1);
		TextView footerCell2 = (TextView) footerView.findViewById(R.id.listFooter_Text2);
		TextView footerCell3 = (TextView) footerView.findViewById(R.id.listFooter_Text3);
		TextView footerCell4 = (TextView) footerView.findViewById(R.id.listFooter_Text4);
		TextView footerCell5 = (TextView) footerView.findViewById(R.id.listFooter_Text5);
		TextView footerCell6 = (TextView) footerView.findViewById(R.id.listFooter_Text6);

		List<Integer> colSizeList = new ArrayList<Integer>();
		colSizeList = getListHeaderWidth();
		if (colSizeList.size() == LIST_COLUMN_NUM) {
			footerCellLabel.setWidth(colSizeList.get(0));
			footerCell1.setWidth(colSizeList.get(1));
			footerCell2.setWidth(colSizeList.get(2));
			footerCell3.setWidth(colSizeList.get(3));
			footerCell4.setWidth(colSizeList.get(4));
			footerCell5.setWidth(colSizeList.get(5));
			footerCell6.setWidth(colSizeList.get(6));
		}
	}

	private void initScoreListBody() {

		List<ScoreManagementScoreListItem> items = new ArrayList<ScoreManagementScoreListItem>();
		List<Integer> colSizeList = new ArrayList<Integer>();
		colSizeList = getListHeaderWidth();

		ListAdapter adapter = new ScoreManagementScoreListAdapter(this, R.layout.score_management_row, items, colSizeList);
		m_ScoreListView.setAdapter(adapter);	
	}

	private void initResultList() {

		List<ScoreManagementResultListItem> items = new ArrayList<ScoreManagementResultListItem>();
		List<Integer> colSizeList = new ArrayList<Integer>();
		colSizeList = getListHeaderWidth();

		/*
		 * Default Value
		 */
		items = setResultListDefaultValue();

		ListAdapter adapter = new ScoreManagementResultListAdapter(this, R.layout.score_management_row, items, colSizeList);
		m_ResultListView.setAdapter(adapter);	
	}

	private List<ScoreManagementResultListItem> setResultListDefaultValue() {
		List<ScoreManagementResultListItem> items = new ArrayList<ScoreManagementResultListItem>();

		List<String> tmp = new ArrayList<String>();
		for (int i=0 ; i < LIST_VARIABLE_COLUMN_NUM ; i++) {
			tmp.add(SCORE_LIST_DEFAULT_PARAMETER);
		}

		items.add(new ScoreManagementResultListItem(getString(R.string.ScoreList_Label_Chip), tmp));
		items.add(new ScoreManagementResultListItem(getString(R.string.ScoreList_Label_Total), tmp));
		items.add(new ScoreManagementResultListItem(getString(R.string.ScoreList_Label_RateTotall) + m_Preference.getString(InitSettingActivity.PREF_KEY_RATING, ""), tmp));
		items.add(new ScoreManagementResultListItem(getString(R.string.ScoreList_Label_StoreMoney), tmp));
		items.add(new ScoreManagementResultListItem(getString(R.string.ScoreList_Label_Money), tmp));

		return items;
	}

	private List<Integer> getListHeaderWidth() {

		List<Integer> ret = new ArrayList<Integer>();
		LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View headerView = inflater.inflate(R.layout.score_management_header, null);

		ret.add(((ImageView) headerView.findViewById(R.id.listHeader_ImageBrank)).getDrawable().getIntrinsicWidth());
		ret.add(((ImageView) headerView.findViewById(R.id.listHeader_Image1)).getDrawable().getIntrinsicWidth());
		ret.add(((ImageView) headerView.findViewById(R.id.listHeader_Image2)).getDrawable().getIntrinsicWidth());
		ret.add(((ImageView) headerView.findViewById(R.id.listHeader_Image3)).getDrawable().getIntrinsicWidth());
		ret.add(((ImageView) headerView.findViewById(R.id.listHeader_Image4)).getDrawable().getIntrinsicWidth());
		ret.add(((ImageView) headerView.findViewById(R.id.listHeader_Image5)).getDrawable().getIntrinsicWidth());
		ret.add(((ImageView) headerView.findViewById(R.id.listHeader_Image6)).getDrawable().getIntrinsicWidth());

		return ret;
	}

	private void setScoreListListener() {

		m_ScoreListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				ScoreManagementScoreListItem tmp = (ScoreManagementScoreListItem) m_ScoreListView.getItemAtPosition(position);
				if (tmp != null) {
					ScoreManagementPopup popupDialog = new ScoreManagementPopup(ScoreManagementActivity.this, m_DrawHandler);
					popupDialog.showScoreUpdateDialog(tmp.getAllCell(), position);
				}
			}
		});

		ImageButton addScoreButton = (ImageButton) findViewById(R.id.listFooter_Button);
		addScoreButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				ScoreManagementPopup popupDialog = new ScoreManagementPopup(ScoreManagementActivity.this, m_DrawHandler);
				popupDialog.showScoreUpdateDialog(new ArrayList<String>(), m_ScoreListView.getCount());
			}
		});
	}

	private void setResultListListener() {
		m_ResultListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch (position) {
				/*
				 * チップと店料金用のレコードタップ時のみポップアップを表示する
				 */
				case ScoreManagementResultListAdapter.INDEX_CHIP_RECORD:
				case ScoreManagementResultListAdapter.INDEX_STORE_MONEY_RECORD:
					ScoreManagementResultListItem item = (ScoreManagementResultListItem) m_ResultListView.getItemAtPosition(position);
					if (item != null) {
						ScoreManagementPopup popupDialog = new ScoreManagementPopup(ScoreManagementActivity.this, m_DrawHandler);
						popupDialog.showResultUpdateDialog(item.getAllCell(), position);
					}
					break;

					/*
					 * その他は処理なし
					 */
				case ScoreManagementResultListAdapter.INDEX_RATE_TOTAL_RECORD:
				case ScoreManagementResultListAdapter.INDEX_TOTAL_RECORD:
				case ScoreManagementResultListAdapter.INDEX_MONEY_RECORD:
					break;
				}

			}
		});
	}

	private void setHandler() {
		m_DrawHandler = new DrawHandler();
	}

	/*
	 * 外部からの描画要求を受けるインナークラス。
	 */
	private class DrawHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {

			Bundle item = msg.getData();
			int type = item.getInt(BUNDLE_KEY_DATA_TYPE);
			String[] tmpArray = item.getStringArray(BUNDLE_KEY_SCORE_DATA);
			List<String> newInputList = Arrays.asList(tmpArray);
			tmpArray = item.getStringArray(BUNDLE_KEY_ACTIVE_DATA);
			List<String> activeDataList = Arrays.asList(tmpArray);
			int position = item.getInt(BUNDLE_KEY_SCORE_POSITION);

			/*
			 * スコア管理リスト描画処理
			 */
			if (type == BUNDLE_KEY_DATA_TYPE_SCORE
					&& newInputList.size() == LIST_VARIABLE_COLUMN_NUM) {


				ScoreManagementScoreListAdapter adapter = getScoreListAdapter();

				List<String> newScoreList = calcInputScore(newInputList, activeDataList);

				if (position == m_ScoreListView.getCount()) { // レコード追加
					adapter.add(new ScoreManagementScoreListItem(newScoreList));
				} else  { // レコード更新
					/*
					 * getViewで得るpositionはFooterも含んでおり、adapterと合わせるため調整する
					 */
					position--;

					ScoreManagementScoreListItem removeObj = adapter.getItem(position);
					adapter.remove(removeObj);
					adapter.insert(new ScoreManagementScoreListItem(newScoreList), position);
				}

				setScoreListFooter();

				reCalcResultList();

				/*
				 * 結果管理リスト描画処理
				 */
			} else if (type == BUNDLE_KEY_DATA_TYPE_RESULT
					&& newInputList.size() == LIST_VARIABLE_COLUMN_NUM) {
				switch (position) {
				/*
				 * チップと店料金用のみポップアップ外部から変更要求を受ける
				 */
				case ScoreManagementResultListAdapter.INDEX_CHIP_RECORD:
					setResultListChip(newInputList);
					break;
				case ScoreManagementResultListAdapter.INDEX_STORE_MONEY_RECORD:
					setResultListStoreMoney(newInputList);
					break;

					/*
					 * その他は処理なし
					 */
				case ScoreManagementResultListAdapter.INDEX_RATE_TOTAL_RECORD:
				case ScoreManagementResultListAdapter.INDEX_TOTAL_RECORD:
				case ScoreManagementResultListAdapter.INDEX_MONEY_RECORD:
					break;
				}

			}
		}
	};

	private List<String> calcInputScore(List<String> inputScore, List<String> activeScore) {

		
		/*
		 * ランキングを付ける
		 */
		List<String> rankList = m_listCalculation.getScoreRanking(inputScore, activeScore);

		/*
		 * 浮枕を算出する
		 */
		List<String> ukimaList = getUkimaList(inputScore, activeScore);

		/*
		 * ランクに応じてウマ値を算出する
		 */
		List<String> umaList = getUmaList(rankList, activeScore);		

		/*
		 * 浮枕とウマ値を合計して点数を算出する
		 */
		List< List<String> > tmp = new ArrayList< List<String> >();
		tmp.add(ukimaList);
		tmp.add(umaList);
		List<String> totalList = m_listCalculation.sumBetweenColumns(tmp);
		
		/*
		 * 一位の点数を算出する
		 */
		List<String> ret = getCalcScoreList(totalList, rankList);
		
		return ret;
	}
	
	private List<String> getCalcScoreList(List<String> list, List<String> rankList) {
		List<String> ret = new ArrayList(list);
		int rank1stIndex = rankList.indexOf("1");
		int score1st = 0;
		for (int i=0 ; i < list.size(); i++) {
			if (i != rank1stIndex) {
				score1st += Integer.parseInt(list.get(i)) * -1;
			}
		}
		ret.set(rank1stIndex, String.format("%d", score1st));
		return ret;
	}


	private List<String> getUmaList(List<String> rankList, List<String> activeScore) {

		List<String> ret = new ArrayList<String>();

		int rank1st = 0;
		int rank2nd = 0;
		int rankBooby = 0;
		int rankWorst = 0;

		String uma = m_Preference.getString(InitSettingActivity.PREF_KEY_UMA, "");
		if (uma.equals(getString(R.string.PreferenceumaArray_1))) {
			rank1st = 0;
			rank2nd = 0;
			rankBooby = 0;
			rankWorst = 0;
		} else if(uma.equals(getString(R.string.PreferenceumaArray_2))) {
			rank1st = 10;
			rank2nd = 5;
			rankBooby = -5;
			rankWorst = -10;
		} else if(uma.equals(getString(R.string.PreferenceumaArray_3))) {
			rank1st = 20;
			rank2nd = 10;
			rankBooby = -10;
			rankWorst = -20;
		}

		int userNum = 0;
		for (int i=0 ; i < activeScore.size() ; i++) {
			if (activeScore.get(i).equals(ScoreManagementPopup.ACTIVE)) {
				userNum++;
			}
		}

		/*
		 * 三人麻雀時のウマ値については考慮の必要あり
		 * 同ランクへのロジックは入っていない。
		 */
		switch (userNum) {
		case 3:
			for (int i=0 ; i < rankList.size() ; i++) {
				if (rankList.get(i).equals("1")) { // ランク一位
					ret.add(String.format("%d", rank1st));
				} else if (rankList.get(i).equals("2")) { // ランクニ位
					ret.add(String.format("%d", rank2nd));
				} else if (rankList.get(i).equals("3")) { // ランク三位
					ret.add(String.format("%d", rankBooby));
				} else {
					ret.add("0");
				}
			}
			break;
			
			/*
			 * 4～6人は同処理
			 */
		case 4:
		case 5:
		case 6:
			for (int i=0 ; i < rankList.size() ; i++) {
				if (rankList.get(i).equals("1")) { // ランク一位
					ret.add(String.format("%d", rank1st));
				} else if (rankList.get(i).equals("2")) { // ランクニ位
					ret.add(String.format("%d", rank2nd));
				} else {
					ret.add("0");
				}
			}
			
			int worstCheckParam = 0;
			int worstIndex = 0;
			for (int i=0 ; i < rankList.size() ; i++) {
				int tmp = Integer.parseInt(rankList.get(i));
				if (tmp > worstCheckParam) {
					worstCheckParam = tmp;
					worstIndex = i;
				}
			}

			int boobyCheckParam = 0;
			int boobyIndex = 0;
			for (int i=0 ; i < rankList.size() ; i++) {
				int tmp = Integer.parseInt(rankList.get(i));
				if (tmp > boobyCheckParam && tmp != worstCheckParam) {
					boobyCheckParam = tmp;
					boobyIndex = i;
				}
			}
			
			ret.set(worstIndex, String.format("%d", rankWorst)); // ランクビリ
			ret.set(boobyIndex, String.format("%d", rankBooby)); // ランクブービー
			
			break;
		}
		
		return ret;
	}

	private List<String> getUkimaList(List<String> inputScore, List<String> activeScore) {

		List<String> ret = new ArrayList<String>();

		/*
		 * 入力値から300減算する
		 */
		List<String> umSubtractionList = new ArrayList<String>();
		for (int i=0 ; i < inputScore.size() ; i++) {
			if (activeScore.get(i).equals(ScoreManagementPopup.ACTIVE)) {
				umSubtractionList.add(UM_SUBTRACTION_VALUE);
			}
		}
		List< List<String> > calcProcess_1 = new ArrayList< List<String> >();
		calcProcess_1.add(inputScore);
		calcProcess_1.add(umSubtractionList);
		List<String> calcProcess_2 = m_listCalculation.sumBetweenColumns(calcProcess_1);


		/*
		 * 入力値から10で除算する
		 */
		List<String> calcProcess_3 = m_listCalculation.divisionColumns(calcProcess_2, UM_DIVISION_VALUE);

		/*
		 * 100点以下を端数設定に従って端数処理を行い浮枕を算出する
		 */
		List<String> ukimaList = new ArrayList<String>();
		Resources resources = getResources();
		String type = m_Preference.getString(InitSettingActivity.PREF_KEY_FRACTION, "");
		if (type.equals(getString(R.string.PreferenceFractionArray_1))) {
			/*
			 * 切り捨て
			 */
			ret = calcTruncation(calcProcess_3);
		} else if (type.equals(getString(R.string.PreferenceFractionArray_2))) {
			/*
			 * 浮きなら切り捨て、沈みなら切り上げ
			 */
			ret = calcTruncationAndRoundedUp(calcProcess_2);
		} else if (type.equals(getString(R.string.PreferenceFractionArray_3))) {
			/*
			 * 四捨五入
			 */
			ret = calcRounding45(calcProcess_3);
		} else if (type.equals(getString(R.string.PreferenceFractionArray_4))) {
			/*
			 * 五捨六入
			 */
			ret = calcRounding56(calcProcess_3);
		} else {
			// I'm a bug
		}
		return ret;
	}

	private List<String> calcTruncation(List<String> list) {

		List<String> ret = new ArrayList<String>();
		for (String val : list) {
			BigDecimal decimal = new BigDecimal(Double.parseDouble(val));
			BigDecimal calcResult = decimal.setScale(0, BigDecimal.ROUND_DOWN); // 小数点第一位まで取得
			ret.add(String.format("%d", (int) calcResult.doubleValue()));
		}
		return ret;
	}

	private List<String> calcTruncationAndRoundedUp(List<String> list) {

		List<String> ret = new ArrayList<String>();
		for (String val : list) {

			BigDecimal decimal = new BigDecimal(Double.parseDouble(val));
			BigDecimal calcResult;
			if (decimal.doubleValue() >= 0) {	// 浮き	
				calcResult = decimal.setScale(0, BigDecimal.ROUND_DOWN); // 小数点第一位まで取得
			} else { // 沈み
				calcResult = decimal.setScale(0, BigDecimal.ROUND_UP); // 小数点第一位まで取得
			}
			ret.add(String.format("%d", (int) calcResult.doubleValue()));

		}
		return ret;
	}

	private List<String> calcRounding45(List<String> list) {

		List<String> ret = new ArrayList<String>();
		for (String val : list) {
			BigDecimal decimal = new BigDecimal(Double.parseDouble(val));
			BigDecimal calcResult = decimal.setScale(0, BigDecimal.ROUND_HALF_UP); // 小数点第一位まで取得
			ret.add(String.format("%d", (int) calcResult.doubleValue()));
		}
		return ret;
	}

	private List<String> calcRounding56(List<String> list) {

		List<String> ret = new ArrayList<String>();
		for (String val : list) {
			BigDecimal decimal = new BigDecimal(Double.parseDouble(val));
			BigDecimal calcResult = decimal.setScale(0, BigDecimal.ROUND_HALF_DOWN); // 小数点第一位まで取得
			ret.add(String.format("%d", (int) calcResult.doubleValue()));
		}
		return ret;
	}

	private void setScoreListFooter() {
		TextView col1 = (TextView) findViewById(R.id.listFooter_Text1);
		TextView col2 = (TextView) findViewById(R.id.listFooter_Text2);
		TextView col3 = (TextView) findViewById(R.id.listFooter_Text3);
		TextView col4 = (TextView) findViewById(R.id.listFooter_Text4);
		TextView col5 = (TextView) findViewById(R.id.listFooter_Text5);
		TextView col6 = (TextView) findViewById(R.id.listFooter_Text6);
		col1.setText(m_listCalculation.sumColumns(getScoreListItemVertical(1)));
		col2.setText(m_listCalculation.sumColumns(getScoreListItemVertical(2)));
		col3.setText(m_listCalculation.sumColumns(getScoreListItemVertical(3)));
		col4.setText(m_listCalculation.sumColumns(getScoreListItemVertical(4)));
		col5.setText(m_listCalculation.sumColumns(getScoreListItemVertical(5)));
		col6.setText(m_listCalculation.sumColumns(getScoreListItemVertical(6)));		
	}

	private List<String> getScoreListFooter() {
		List<String> ret = new ArrayList<String>();
		TextView col1 = (TextView) findViewById(R.id.listFooter_Text1);
		TextView col2 = (TextView) findViewById(R.id.listFooter_Text2);
		TextView col3 = (TextView) findViewById(R.id.listFooter_Text3);
		TextView col4 = (TextView) findViewById(R.id.listFooter_Text4);
		TextView col5 = (TextView) findViewById(R.id.listFooter_Text5);
		TextView col6 = (TextView) findViewById(R.id.listFooter_Text6);
		ret.add(col1.getText().toString());
		ret.add(col2.getText().toString());
		ret.add(col3.getText().toString());
		ret.add(col4.getText().toString());
		ret.add(col5.getText().toString());
		ret.add(col6.getText().toString());
		return ret;
	}

	private ScoreManagementScoreListAdapter getScoreListAdapter() {
		/*
		 * ListView.addHeaderViewを実行するとListView.getAdapter()がWrapperListAdapterを返すようになる。
		 * 独自Adapterを取得するにはWrapperListAdapter.getWrappedAdapterを呼ばないとダメ。
		 */
		WrapperListAdapter tmp = (WrapperListAdapter) m_ScoreListView.getAdapter();
		ScoreManagementScoreListAdapter ret = (ScoreManagementScoreListAdapter) tmp.getWrappedAdapter();
		return ret;
	}

	private ScoreManagementResultListAdapter getResultListAdapter() {
		return (ScoreManagementResultListAdapter) m_ResultListView.getAdapter();
	}

	/*
	 * スコア管理用リスト 指定セルデータ設定処理
	 */
	private void setScoreListItem(String data, int positionX, int positionY) {

	}


	/*
	 * スコア管理用リスト 指定カラムデータ設定処理
	 */
	private void setScoreListItemVertical(List<String> list, int positionX) {

	}

	/*
	 * スコア管理用リスト 指定レコードデータ設定処理
	 */
	private void setScoreListItemHorizontal(List<String> list, int positionY) {

		ScoreManagementResultListAdapter adapter = getResultListAdapter();
		ScoreManagementResultListItem removeObj = adapter.getItem(positionY);
		ScoreManagementResultListItem insertObj = removeObj;

		insertObj.setCell1(list.get(0));
		insertObj.setCell2(list.get(1));
		insertObj.setCell3(list.get(2));
		insertObj.setCell4(list.get(3));
		insertObj.setCell5(list.get(4));
		insertObj.setCell6(list.get(5));

		adapter.remove(removeObj);
		adapter.insert(insertObj, positionY);
	}

	/*
	 * 結果用リスト 指定セルデータ設定処理
	 */
	private void setResultListItem(String data, int positionX, int positionY) {

		ScoreManagementResultListAdapter adapter = getResultListAdapter();
		ScoreManagementResultListItem removeObj = adapter.getItem(positionY);
		ScoreManagementResultListItem insertObj = removeObj;

		switch (positionX) {
		case 1:
			insertObj.setCell1(data);
			break;
		case 2:
			insertObj.setCell2(data);
			break;
		case 3:
			insertObj.setCell3(data);
			break;
		case 4:
			insertObj.setCell4(data);
			break;
		case 5:
			insertObj.setCell5(data);
			break;
		case 6:
			insertObj.setCell6(data);
			break;
		}

		adapter.remove(removeObj);
		adapter.insert(insertObj, positionY);
	}


	/*
	 * 結果用リスト 指定カラムデータ設定処理
	 */
	private void setResultListItemVertical(List<String> list, int positionX) {

	}


	/*
	 * 結果用リスト 指定レコードデータ設定処理
	 */
	private void setResultListItemHorizontal(List<String> list, int positionY) {

	}	

	/*
	 * スコア管理用リスト 指定セルデータ取得処理
	 */
	private String getScoreListItem(int positionX, int positionY) {

		String ret = new String("");

		ScoreManagementScoreListAdapter adapter = getScoreListAdapter();
		ScoreManagementScoreListItem item = adapter.getItem(positionY);
		if (item != null) {
			switch (positionX) {
			case 1:
				ret = item.getCell1();
				break;
			case 2:
				ret = item.getCell2();
				break;
			case 3:
				ret = item.getCell3();
				break;
			case 4:
				ret = item.getCell4();
				break;
			case 5:
				ret = item.getCell5();
				break;
			case 6:
				ret = item.getCell6();
				break;
			default :
				Log.w(getCommonLogTag(), "Item Not Found");
				break;
			}
		}

		return ret;
	}

	/*
	 * スコア管理用リスト 指定カラムデータ取得処理
	 */
	private List<String> getScoreListItemVertical(int positionX) {
		List<String> ret = new ArrayList<String>();

		ScoreManagementScoreListAdapter adapter = getScoreListAdapter();
		for (int i=0 ; i < adapter.getCount() ; i++) {
			String tmp = getScoreListItem(positionX, i);
			ret.add(tmp);
		}

		return ret;
	}

	/*
	 * スコア管理用リスト 指定レコードデータ取得処理
	 */
	private List<String> getScoreListItemHorizontal(int positionY) {
		List<String> ret = new ArrayList<String>();

		String tmp1 = getScoreListItem(1, positionY);
		String tmp2 = getScoreListItem(2, positionY);
		String tmp3 = getScoreListItem(3, positionY);
		String tmp4 = getScoreListItem(4, positionY);
		String tmp5 = getScoreListItem(5, positionY);
		String tmp6 = getScoreListItem(6, positionY);
		if ((tmp1 != null) && 
				(tmp2 != null) && 
				(tmp3 != null) && 
				(tmp4 != null) && 
				(tmp5 != null) && 
				(tmp6 != null)) {
			ret.add(tmp1);
			ret.add(tmp2);
			ret.add(tmp3);
			ret.add(tmp4);
			ret.add(tmp5);
			ret.add(tmp6);
		}
		return ret;
	}
	/*
	 * リザルト用リスト 指定セルデータ取得処理
	 */
	private String getResultListItem(int positionX, int positionY) {

		String ret = new String("");

		ScoreManagementResultListAdapter adapter = getResultListAdapter();
		ScoreManagementResultListItem item = adapter.getItem(positionY);
		if (item != null) {
			switch (positionX) {
			case 1:
				ret = item.getCell1();
				break;
			case 2:
				ret = item.getCell2();
				break;
			case 3:
				ret = item.getCell3();
				break;
			case 4:
				ret = item.getCell4();
				break;
			case 5:
				ret = item.getCell5();
				break;
			case 6:
				ret = item.getCell6();
				break;
			default :
				Log.w(getCommonLogTag(), "Item Not Found");
				break;
			}
		}

		return ret;
	}

	/*
	 * リザルト用リスト 指定カラムデータ取得処理
	 */
	private List<String> getResultListItemVertical(int positionX) {
		List<String> ret = new ArrayList<String>();

		ScoreManagementResultListAdapter adapter = getResultListAdapter();
		for (int i=0 ; i < adapter.getCount() ; i++) {
			String tmp = getResultListItem(positionX, i);
			ret.add(tmp);
		}

		return ret;
	}

	/*
	 * リザルト用リスト 指定レコードデータ取得処理
	 */
	private List<String> getResultListItemHorizontal(int positionY) {
		List<String> ret = new ArrayList<String>();

		String tmp1 = getResultListItem(1, positionY);
		String tmp2 = getResultListItem(2, positionY);
		String tmp3 = getResultListItem(3, positionY);
		String tmp4 = getResultListItem(4, positionY);
		String tmp5 = getResultListItem(5, positionY);
		String tmp6 = getResultListItem(6, positionY);
		if ((tmp1 != null) && 
				(tmp2 != null) && 
				(tmp3 != null) && 
				(tmp4 != null) && 
				(tmp5 != null) && 
				(tmp6 != null)) {
			ret.add(tmp1);
			ret.add(tmp2);
			ret.add(tmp3);
			ret.add(tmp4);
			ret.add(tmp5);
			ret.add(tmp6);
		}
		return ret;
	}

	private void setResultListChip(List<String> item) {

		ScoreManagementResultListAdapter adapter = getResultListAdapter();
		ScoreManagementResultListItem removeObj = adapter.getItem(ScoreManagementResultListAdapter.INDEX_CHIP_RECORD);
		adapter.remove(removeObj);
		adapter.insert(new ScoreManagementResultListItem(getString(R.string.ScoreList_Label_Chip), item),
				ScoreManagementResultListAdapter.INDEX_CHIP_RECORD);

		reCalcResultList();
	}

	private void setResultListTotal() {

		List<String> scoreList = getScoreListFooter();
		List<String> chipList = getResultListItemHorizontal(ScoreManagementResultListAdapter.INDEX_CHIP_RECORD);
		List< List<String> > tmp = new ArrayList<List<String>>();
		tmp.add(scoreList);
		tmp.add(chipList);
		List<String> calcResult = m_listCalculation.sumBetweenColumns(tmp);

		setScoreListItemHorizontal(calcResult, ScoreManagementResultListAdapter.INDEX_TOTAL_RECORD);
	}

	private void setResultListRateTotal() {

		List<String> totalList = getResultListItemHorizontal(ScoreManagementResultListAdapter.INDEX_TOTAL_RECORD);
		int rate = Integer.parseInt(m_Preference.getString(InitSettingActivity.PREF_KEY_RATING, "0"));
		List<String> calcResult = m_listCalculation.multiplicationColumns(totalList, rate);

		setScoreListItemHorizontal(calcResult, ScoreManagementResultListAdapter.INDEX_RATE_TOTAL_RECORD);
	}

	private void setResultListStoreMoney(List<String> item) {

		ScoreManagementResultListAdapter adapter = getResultListAdapter();
		ScoreManagementResultListItem removeObj = adapter.getItem(ScoreManagementResultListAdapter.INDEX_STORE_MONEY_RECORD);
		adapter.remove(removeObj);
		adapter.insert(new ScoreManagementResultListItem(getString(R.string.ScoreList_Label_StoreMoney), item),
				ScoreManagementResultListAdapter.INDEX_STORE_MONEY_RECORD);

		reCalcResultList();
	}

	private void setResultListMoney() {

		List<String> rateTotalList = getResultListItemHorizontal(ScoreManagementResultListAdapter.INDEX_RATE_TOTAL_RECORD);
		List<String> storeMoneyList = getResultListItemHorizontal(ScoreManagementResultListAdapter.INDEX_STORE_MONEY_RECORD);
		List< List<String> > tmp = new ArrayList<List<String>>();
		tmp.add(rateTotalList);
		tmp.add(storeMoneyList);
		List<String> calcResult = m_listCalculation.sumBetweenColumns(tmp);

		setScoreListItemHorizontal(calcResult, ScoreManagementResultListAdapter.INDEX_MONEY_RECORD);
	}

	private void reCalcResultList() {
		setResultListTotal();
		setResultListRateTotal();
		setResultListMoney();
	}


	/*
	 * ・DBデータ形式について
	 * 　一時スコアテーブル：係数未計算値を登録。特殊加数は区分を変えて登録。
	 * 　　カラム：ゲームID(※)、区分、点数、ユーザ名、レコード数
	 * 　※開始時の日時をゲームIDとする
	 * ・新規点数表の表示
	 * ・ゲーム新規開始ボタン
	 * ○点数追加画面
	 * ・ゲーム終了ボタン
	 * ○空レコードタップ時は追加で、登録済みレコードタップ時は更新。どちらも同じIFで編集できるように工夫。
	 * ○縦の計算メソッド
	 * ・係数の計算メソッド
	 * ・DBデータ取得
	 * ・DBデータ保存
	 * ・DBデータの格納
	 * ・ユーザ名非更新時は前回点数を表示
	 * ・ユーザ名更新時は新規点数を表示
	 * ・ウマだか、浮きだかの計算ロジックの追加
	 * ・点数入力IFを独自の数値入力ダイアログに変更する。(今のはマイナスが入力できない)
	 */

}
